package top.zhangjianyong.blog.config.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 过滤器配置
 *
 * @author HDN
 * @since 2024/7/28 下午5:06
 */
@Configuration
public class FilterConfig {

    @Autowired
    private AntiHotlinkFilter antiHotlinkFilter;

    @Bean
    public FilterRegistrationBean<UserFilter> userFilterFilterRegistrationBean() {
        FilterRegistrationBean<UserFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new UserFilter());
        filterRegistrationBean.setName("user-filter");
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(1); // 用户过滤器优先级更高
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<AntiHotlinkFilter> antiHotlinkFilterFilterRegistrationBean() {
        FilterRegistrationBean<AntiHotlinkFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(antiHotlinkFilter);
        filterRegistrationBean.setName("anti-hotlink-filter");
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(2); // 防盗链过滤器优先级较低
        return filterRegistrationBean;
    }
}
